<html>
<head><meta charset="utf-8"><title>Is getting address of generic functions makes sense? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html">Is getting address of generic functions makes sense?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206099964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20getting%20address%20of%20generic%20functions%20makes%20sense%3F/near/206099964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html#206099964">(Aug 06 2020 at 02:50)</a>:</h4>
<p>The mistakes I often make is forgot to call the generic functions with <code>()</code>.<br>
For example, consider the <code>std::mem::size_of</code> function:</p>
<div class="codehilite"><pre><span></span><code><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">TypeA</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">TypeB</span><span class="o">&gt;</span><span class="p">);</span><span class="w"></span>
</code></pre></div>


<p>The error message in that case is <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=4130d03247e89e9ebd43f0e3bd55b488">not great</a>:</p>
<div class="codehilite"><pre><span></span><code><span class="n">error</span><span class="p">[</span><span class="n">E0369</span><span class="p">]</span>: <span class="nc">binary</span><span class="w"> </span><span class="n">operation</span><span class="w"> </span><span class="err">`</span><span class="o">==</span><span class="err">`</span><span class="w"> </span><span class="n">cannot</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">applied</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="k">type</span> <span class="err">`</span><span class="k">fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">16</span><span class="p">]</span><span class="o">&gt;</span><span class="p">}</span><span class="err">`</span><span class="w"></span>
<span class="w">  </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">main</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">17</span>:<span class="mi">5</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w"></span>
<span class="mi">17</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">Left</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">Right</span><span class="o">&gt;</span><span class="w"> </span><span class="p">);</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w">     </span><span class="o">^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w">     </span><span class="o">|</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w">     </span><span class="k">fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">16</span><span class="p">]</span><span class="o">&gt;</span><span class="p">}</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w">     </span><span class="k">fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="p">[</span><span class="kt">u16</span><span class="p">;</span><span class="w"> </span><span class="mi">8</span><span class="p">]</span><span class="o">&gt;</span><span class="p">}</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w"></span>
<span class="w">   </span><span class="o">=</span><span class="w"> </span><span class="n">note</span>: <span class="nc">this</span><span class="w"> </span><span class="n">error</span><span class="w"> </span><span class="n">originates</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="kr">macro</span><span class="w"> </span><span class="p">(</span><span class="k">in</span><span class="w"> </span><span class="n">Nightly</span><span class="w"> </span><span class="n">builds</span><span class="p">,</span><span class="w"> </span><span class="n">run</span><span class="w"> </span><span class="n">with</span><span class="w"> </span><span class="o">-</span><span class="n">Z</span><span class="w"> </span><span class="kr">macro</span><span class="o">-</span><span class="n">backtrace</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">more</span><span class="w"> </span><span class="n">info</span><span class="p">)</span><span class="w"></span>
</code></pre></div>


<p>I wonder if we could have a lint to warn about comparing address of generic functions.</p>



<a name="206100136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20getting%20address%20of%20generic%20functions%20makes%20sense%3F/near/206100136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html#206100136">(Aug 06 2020 at 02:54)</a>:</h4>
<p>I don't think it needs a lint, just a better diagnostic</p>



<a name="206100140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20getting%20address%20of%20generic%20functions%20makes%20sense%3F/near/206100140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html#206100140">(Aug 06 2020 at 02:54)</a>:</h4>
<p>'try calling the function with <code>()</code>'</p>



<a name="206105351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20getting%20address%20of%20generic%20functions%20makes%20sense%3F/near/206105351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html#206105351">(Aug 06 2020 at 05:07)</a>:</h4>
<p>That solve a partial problem. Consider this code: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b680c8d686b6318df252a92f75d0e11f">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b680c8d686b6318df252a92f75d0e11f</a></p>
<div class="codehilite"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">size_of</span><span class="p">;</span><span class="w"></span>
<span class="cp">#[repr(C)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">a</span>: <span class="kt">u32</span> <span class="p">}</span><span class="w"></span>

<span class="k">extern</span><span class="w"> </span><span class="s">&quot;C&quot;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">ffi_call</span><span class="p">(</span><span class="n">o</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">Foo</span><span class="p">,</span><span class="w"> </span><span class="n">size</span>: <span class="kt">usize</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">ffi_call</span><span class="p">(</span><span class="w"></span>
<span class="w">            </span><span class="o">&amp;</span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">a</span>: <span class="mi">42</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">Foo</span><span class="o">&gt;</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"></span>
<span class="w">        </span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>The <code>size_of::&lt;Foo&gt;</code> should be called, instead here its <code>fn</code> address casted to usize.</p>



<a name="206139242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20getting%20address%20of%20generic%20functions%20makes%20sense%3F/near/206139242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html#206139242">(Aug 06 2020 at 13:20)</a>:</h4>
<p>note also that I believe we tag functions as unnamed_addr to LLVM, so this is not guaranteed to return true (or false, for that matter)</p>



<a name="206139295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20getting%20address%20of%20generic%20functions%20makes%20sense%3F/near/206139295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html#206139295">(Aug 06 2020 at 13:20)</a>:</h4>
<p>getting the address of a function can still be useful and I don't think a rustc lint for that makes sense</p>



<a name="206338265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20getting%20address%20of%20generic%20functions%20makes%20sense%3F/near/206338265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html#206338265">(Aug 08 2020 at 08:23)</a>:</h4>
<p>getting the address of a function is meaningful but comparing addresses generally is not</p>



<a name="206338272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20getting%20address%20of%20generic%20functions%20makes%20sense%3F/near/206338272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html#206338272">(Aug 08 2020 at 08:23)</a>:</h4>
<p>also see <a href="https://github.com/rust-lang/rust/issues/54685">https://github.com/rust-lang/rust/issues/54685</a>, <a href="https://github.com/rust-lang/rust/issues/70861">https://github.com/rust-lang/rust/issues/70861</a></p>



<a name="206338274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20getting%20address%20of%20generic%20functions%20makes%20sense%3F/near/206338274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html#206338274">(Aug 08 2020 at 08:23)</a>:</h4>
<p>and then we got <a href="https://github.com/rust-lang/rust/issues/73722">https://github.com/rust-lang/rust/issues/73722</a> which is really interesting and potentially problematic</p>



<a name="206926679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20getting%20address%20of%20generic%20functions%20makes%20sense%3F/near/206926679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20getting.20address.20of.20generic.20functions.20makes.20sense.3F.html#206926679">(Aug 14 2020 at 13:03)</a>:</h4>
<p>submit a lint issue in clippy: <a href="https://github.com/rust-lang/rust-clippy/issues/5906">https://github.com/rust-lang/rust-clippy/issues/5906</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>